<% layout("/layouts/micro.html",{title:"用户管理管理"}){ %>
<div class="childrenBody">
	<div id="importBox" style="display: none">
		<form id="importForm" action="${ctx}/sys/user/import" method="post" enctype="multipart/form-data"
			  class="layui-form" style="padding-left:20px;text-align:center;" onsubmit="layer.load(0, {shade: false});"><br/>
			<input id="uploadFile" name="file" type="file" style="width:330px"/><br/><br/>　　
			<input id="btnImportSubmit" class="layui-btn" type="submit" value="导入"/>
			<a class="layui-btn layui-btn-primary" href="${ctx}/sys/user/import/template">下载模板</a>
		</form>
	</div>
	<div class="kit-table">
		<form class="layui-form">
			<div class="kit-table-header">
				<div class="kit-search-btns">
					<%if(shiro.hasPermission("sys:user:edit")){%>
					<a class="layui-btn layui-btn-normal adduser_btn"><i class="layui-icon">&#xe608;</i>新增</a>
					<a class="layui-btn layui-btn-normal btnExport">导出</a>
					<a class="layui-btn layui-btn-normal btnImport">导入</a>
					<a class="layui-btn layui-btn-danger layui-btn-normal delAll_btn"><i class="layui-icon">&#xe640;</i>批量删除</a>
					<%}%>
				</div>
				<div class="kit-search-inputs clearfix">
					<div class="kit-search-keyword">
						<input type="text" class="layui-input" id="keyword" name="keyword"
							   placeholder="登录名/姓名" />
						<button class="search_btn" type="button">
							<i class="layui-icon">&#xe615;</i>
						</button>
					</div>
				</div>
			</div>
		</form>
		<div class="kit-table-body">

			<table id="userList" lay-filter="userList"></table>

			<!--操作-->
			<script type="text/html" id="userListBar">
				<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
				<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
			</script>
		</div>
	</div>
</div>

<script>
    layui.use(['form','layer','laydate','table','laytpl'],function(){
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            laydate = layui.laydate,
            laytpl = layui.laytpl,
            table = layui.table;
        //列表
        var tableIns = table.render({
            elem: '#userList',
            url : '${ctx}/sys/user/listAjax?officeId=${user.office.id!}',
            cellMinWidth : 95,
            page : true,
            request: {
                pageName: 'pageNo', //页码的参数名称，默认：page
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            height : "full-70",
            limit : 15,
            limits : [10,15,20,25],
            id : "userListTable",
            cols : [[
                {type: "checkbox", fixed:"left", width:50},
                {field:'companyName', title: '归属公司'},
                {field:'officeName', title: '归属部门'},
                {field:'loginName', title: '登录名'},
                {field:'name', title: '姓名'},
                {field:'phone', title: '电话'},
                {field:'mobile', title: '手机'},
            <%if(shiro.hasPermission("sys:user:edit")){%>
            {title: '操作', width:170, templet:'#userListBar',fixed:"right",align:"center"}
        <%}%>
    ]]
    });

        //搜索
        $(".search_btn").on("click",function(){
            var keyword = $('#keyword');
            table.reload("userListTable",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    keyword : keyword.val(),
                }
            })
        });

        //添加
        function adduser(id){
            var index = layui.layer.open({
                    title : "添加用户信息",
                    type : 2,
                    maxmin:true,
                    content : "${ctx}/sys/user/form?id="+id,
                    area: ['90%', '75%'],
					<%if(shiro.hasPermission("sys:user:edit")){%>
					btn: ['保存', '取消'],
						yes: function(index, layero) {
							var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法;
							iframeWin.saveForm();
						},
						end:function(){
                            tableIns.reload();
						}
					<%}%>
				})
        }
        $(".adduser_btn").click(function(){
            var id = '${user.id}';
            adduser(id);
        })

        $(".btnExport").click(function(){
            layer.confirm('确认要导出用户数据吗？', {
                btn: ['确定','取消'] //按钮
            }, function(index){
                layer.close(index);
                $("#searchForm").attr("action","${ctx}/sys/user/export");
                $("#searchForm").submit();
            }, function(index){
                layer.close(index);
            });
        });
        $(".btnImport").click(function(){
            layer.open({
                type: 1,
                title: '导入数据',
                area: ['400px', '230px'], //宽高
                content: $("#importBox").html(),
                btn:['关闭']
            });
        });


        //批量删除
        $(".delAll_btn").click(function(){
            var checkStatus = table.checkStatus('userListTable'),
                data = checkStatus.data,
                selectedIds = [];
            if(data.length > 0) {
                for (var i in data) {
                    selectedIds.push(data[i].id);
                }
                layer.confirm('确定删除选中的数据？', {icon: 3, title: '提示信息'}, function (index) {
                    $.post("${ctx}/sys/user/batchDelete",{selectedIds:selectedIds.join(",")},function(result){
                        if(result.code=="0"){
                            layer.msg(result.msg);
                        }else{
                            layer.msg(result.msg);
                        }
                        tableIns.reload();
                        layer.close(index);
                    });
                })
            }else{
                layer.msg("请选择需要删除的数据");
            }
        })

        table.on('tool(userList)', function(obj){
            var layEvent = obj.event,
                data = obj.data;

            if(layEvent === 'edit'){ //编辑
                adduser(data.id);
            }else if(layEvent === 'del'){ //删除
                layer.confirm('确定删除选中数据？',{icon:3, title:'提示信息'},function(index){
                    $.post("${ctx}/sys/user/delete",{id:data.id},function(result){
                        if(result.code=="0"){
                            layer.msg(result.msg);
                        }else{
                            layer.msg(result.msg);
                        }
                        tableIns.reload();
                        layer.close(index);
                    });
                });
            }else if(layEvent === 'look'){
                parent.parent.tab.tabAdd({
                    "id": data.id,
                    "title": data.name+'--个人主页',
                    "icon": 'fa fa-user-circle',
                    "url": '/legal/member/index?id='+data.id
                });
            }
        });

    })
</script>
<%}%>